package elka.sag.logisticsystem.actor;

import org.apache.log4j.Logger;

import elka.sag.logisticsystem.bean.ActionStatus;
import elka.sag.logisticsystem.bean.Order;
import elka.sag.logisticsystem.message.SimpleMessage;

public class DeliveryActor extends BaseActor {
	private final static String RULE_PATH = "deliveryRules.drl";
	private final static Logger LOGGER = Logger.getLogger(DeliveryActor.class);
	
	
	@Override
	public void preStart() {
		super.preStart();
		ruleService.loadRules(RULE_PATH, getClass().getName());
	}
	
	@Override
	protected void processMessage(SimpleMessage message) throws Exception {
		//dla symulacji przetwarzania czekamy przez losowy czas 0-10s
		int waitForTime = (int) (Math.random() * 1500);
		Thread.sleep(waitForTime);
		
		//tutaj nic nie robimy, tylko log i wyznaczenie nowego statusu
		LOGGER.info(getClass().getSimpleName() + ": Wyslanie zamowienia, orderId: " + message.getOrderId());
		
		Order order = repositoryService.getOrderById(message.getOrderId());
		ActionStatus actionStatus = ActionStatus.OK;
		
		LOGGER.info(getClass().getSimpleName() + ": Wyslano zamowienie, orderId: " + message.getOrderId());
		
		Order.OrderStatus newStatus = getNextOrderStatus(order, actionStatus);
		order.setStatus(newStatus);
		order.setProcess(false);
		
		//tutaj tylko zmiana statusu na nowy
		repositoryService.updateOrderStatusAndProcess(order);
	}

}
